package com.jn.hilt;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.jn.hilt.entity.User;
import com.jn.hilt.manager.UserManager;
import com.jn.hilt.manager.UserManagerIntParams;
import com.jn.hilt.manager.UserManagerParams;
import com.jn.hilt.qualifier.AppQualifier;
import com.jn.hilt.utils.LogUtils;

import dagger.hilt.android.AndroidEntryPoint;
import jakarta.inject.Inject;

// 标记为 Hilt 管理的 Activity
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // 注入业务类依赖
    @Inject
    public UserManager mUserManager;
    @Inject
    public UserManager mUserManager1;
    @Inject
    public LogUtils mLogUtils;

    // 注入 SharedPreferences（来自 AppModule）
    @Inject
    public SharedPreferences mSharedPreferences;

    // 注入应用版本（来自 AppModule）
    @Inject
    String appVersion;

    //注入带限定符的依赖
    @Inject
    @AppQualifier.LinearAnim
    TranslateAnimation linearAnim;

    @Inject
    @AppQualifier.OvershootAnim
    TranslateAnimation overShootAnim;
    @Inject
    UserManagerParams mUserManagerParams;
    @Inject
    UserManagerIntParams mUserManagerIntParams;

    private TextView mText1;
    private TextView mText2;
    private LinearLayout mMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        mText1 = findViewById(R.id.text1);
        mText2 = findViewById(R.id.text2);
        mMain = findViewById(R.id.main);
        mText1.setOnClickListener(this);
        mText2.setOnClickListener(this);
        mMain.setOnClickListener(this);


        // 使用注入的 UserManager 创建用户
        User user = mUserManager.createUser("id", "name", 88556633, "zz");
        String userInfo = mUserManager.formatUserInfo(user);
        mLogUtils.debug(userInfo);
        // 保存到 SharedPreferences
        mSharedPreferences.edit().putString("last_user", userInfo).apply();
        String sp_user = mSharedPreferences.getString("last_user", "111");
        mLogUtils.debug(sp_user);
        mLogUtils.debug("当前版本号:" + appVersion);
        mLogUtils.debug(appVersion.length() + "");

        mLogUtils.debug("mUserManager hash == " + mUserManager.hashCode());
        mLogUtils.debug("mUserManager1 hash == " + mUserManager1.hashCode());
        mLogUtils.debug("mLogUtils hash == " + mLogUtils.hashCode());

        mLogUtils.debug("mSharedPreferences hash == " + mSharedPreferences.hashCode());
        mLogUtils.debug("linearAnim hash == " + linearAnim.hashCode());
        mLogUtils.debug("overShootAnim hash == " + overShootAnim.hashCode());

        mLogUtils.debug(mUserManagerParams.getStoredUser().toString());

        mLogUtils.debug("mUserManagerIntParams hash == " + mUserManagerIntParams.hashCode());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.text1) {
            mText1.startAnimation(linearAnim);
        } else if (id == R.id.text2) {
            mText2.startAnimation(overShootAnim);
        } else if (id == R.id.main) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    }
}